package com.qk.controller;

import com.qk.common.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import com.qk.utils.AliyunOSSOperator;

import java.util.UUID;

@Slf4j
@RestController
public class UploadController {
/*    @PostMapping("/upload")
    public Result upload(String username , Integer  age ,MultipartFile file) throws Exception {
            log.info("上传文件开始...{}, {}, {}", username, age, file);

            String originalFilename = file.getOriginalFilename();
            String fileName = originalFilename .substring(originalFilename.lastIndexOf("."));
            String newFileName = UUID.randomUUID() .toString() + fileName;

            file.transferTo(new File("D:/images/" + newFileName ));
            return Result.success();
    }*/

    @Autowired
    public AliyunOSSOperator aliyunOSSOperator;
    @PostMapping("/upload")
    public Result upload(MultipartFile image) throws Exception{
        log.info("上传文件开始...{}", image);
        String originalFilename = image.getOriginalFilename();
        String fileName = originalFilename .substring(originalFilename.lastIndexOf("."));

        String newFileName = UUID.randomUUID() .toString() + fileName;
        String url = aliyunOSSOperator.upload(image.getBytes(), newFileName);
        return Result.success(url);

    }
}
